package com.zjc.yohoshop.mvp.view.custom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.TextView;

@SuppressLint("AppCompatCustomView")
public class MyTextViewGoodsValuesBottom extends TextView {

    private boolean isSelect = false;

    private int sumHeight;//获取控件总高
    private int sumWidth;//获取控件总宽

    private Paint paint_bg;//背景画笔
    private Paint paint_text;//文字画笔


    public MyTextViewGoodsValuesBottom(Context context) {
        super(context);
    }

    public MyTextViewGoodsValuesBottom(Context context, AttributeSet attrs) {  //如没有  AttributeSet 无法再XMl  中使用  设置自定义属性
        super(context, attrs);
        initPaint();
    }

    public void setIsSelect(boolean isSelect) {
        this.isSelect = isSelect;
        invalidate();
    }

    //初始化画笔
    private void initPaint() {
        //初始化背景画笔
        paint_bg = new Paint();
        paint_bg.setColor(Color.BLACK);
        paint_bg.setStyle(Paint.Style.FILL);
        paint_bg.setStrokeWidth(5);

        //初始化文字画笔
        paint_text = new Paint();
        paint_text.setTextAlign(Paint.Align.CENTER);
        paint_text.setColor(Color.parseColor("#BCBDBC"));
        paint_text.setTextSize(30);
    }

    public MyTextViewGoodsValuesBottom(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyTextViewGoodsValuesBottom(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        sumHeight = getMeasuredHeight();
        sumWidth = getMeasuredWidth();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        if (isSelect){
            paint_text.setColor(Color.BLACK);
            paint_text.setStrokeWidth(4);
            //绘制底部背景色
            RectF rectF = new RectF(0, sumHeight - 2, sumWidth, sumHeight);
            canvas.drawRect(rectF, paint_bg);
        }else{
            paint_text.setColor(Color.parseColor("#C0C0C0"));
            paint_text.setStrokeWidth(2);
        }
        paint_text.setTextSize(sumHeight / 3);
        canvas.drawText(getText().toString(), (sumWidth / 2), (sumHeight / 2 + sumHeight / 4), paint_text);


    }


}
